
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  query.groupby;

import java.util.Hashtable;
import java.util.Vector;

import logger.PoolLogger;

import utility.ConvertToLower;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.GetIdName;

public class Expression  implements  Groupbyinterface
{
	GetIdName gid;
	String buffer[];
	Hashtable<Object, Object> table = new Hashtable<Object, Object>();
	String Expression="expression";
	Vector<Object> expression = new Vector<Object>();
	String Field;
	Hashtable<Object,Object> ReportMap =new Hashtable<Object, Object>();
	String typeofreport;
	String Key;
	String Value;
	String ReportName;
	PoolLogger pl;
	
	public Expression() {
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	public String[] getBuffer() 
	{
		return buffer;
	}

	public void getResult() 
	{
		boolean newcolumnflag=false;
		Expression = table.get(Expression).toString();
		String splitbuffer[] = buffer[0].split("\t");
		for(int i=0;i<splitbuffer.length;i++)
			expression.add(splitbuffer[i]);
		String NewColumn = "";
		int indexofcolumn = -1 ;
		if(table.containsKey("column"))
		{
			NewColumn = table.get("column").toString();
			indexofcolumn = expression.indexOf(NewColumn);
		}	
		else
		{
			NewColumn = table.get("key").toString() + " "  +  table.get("value").toString();
			newcolumnflag = true;
		}
		if(indexofcolumn==-1)
		{	
			String heading[]=  buffer[0].split("\t");
			indexofcolumn = heading.length+1;
			buffer[0] =buffer[0]+NewColumn+"\t";
		}
		calculation.Expression exp = new calculation.Expression(new Hashtable<Object, Object>(),new Hashtable<Object, Object>());
		
		for(int i=1;i<buffer.length;i++)
		{
			splitbuffer = buffer[i].split("\t");
			for(int j=0;j<expression.size();j++)
			{	
				if(Expression.contains(expression.get(j).toString()))
					Expression = Expression.replace(expression.get(j).toString(), splitbuffer[j]);
			}
			Double result = exp.parse(Expression);
			if(!newcolumnflag)
			{	
				buffer[i] = buffer[i]+Double.toString(result)+"\t";
			}
			
		}
		
	}

	public void setBuffer(String[] Buffer) 
	{
		this.buffer = Buffer;
	}

	public void initPool() 
	{
		gid =new GetIdName();
	}

	public void setobject(String object) 
	{
		table.put("mid", object);
		table.put("td", "null");
		RecordSearch rs =new RecordSearch();
		rs.setConditionMap(table);
		String line[] =rs.getArrayFromResultSet();
		table.clear();
		for(int i=0;i<line.length;i++)
		{
			String splitline[] = line[i].split("\t");
			String value = (splitline[Column.pv_index-1]);
			if(value.equals("0"))
				value = splitline[Column.vt_index-1];
			else
				value = gid.getItem(value);
			table.put(gid.getItem(splitline[Column.pid_index-1]),value);
		}
		table = ConvertToLower.convertHashKey(table);
	}

	public void setField(String Field)
	{
		this.Field = Field;
	}

	public String getheading() 
	{
		return null;
	}

	public String getField() 
	{
		return null;
	}

	public void setheading(String heading) 
	{
		
	}

	public void addReport() 
	{
	/*	try 
		{
			Typereport typereport;
			Class C = Class.forName("combinereport.condition.typereport."+CapitalChar.makeFirstCharCapital(typeofreport));
			typereport = (Typereport) C.newInstance();
			typereport.setData(Key, Value, database, ReportMap);
			String path=typereport.getPath(ReportName);
			StringBuffer datafromfile = new StringBuffer();
			java.io.File file =new java.io.File(path); 
			RandomAccessFile raf =new RandomAccessFile(file,"r");
			String line;
			while((line=raf.readLine())!=null)
			{
				datafromfile.append(line+"\n");
			}
			file.deleteOnExit();
			for(int i=0;i<buffer.length;i++)
			{
				datafromfile.append(buffer[i]+"\t");
			}
			buffer = datafromfile.toString().split("\t");
		}
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		}
		catch (InstantiationException e) 
		{	
			e.printStackTrace();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}*/
		getResult();
	}

	public void updateReport() 
	{
		getResult();
	}
	public void setReportMap(Hashtable<Object, Object> ReportMap) 
	{
	 this.ReportMap = ReportMap;	
	}
	public void setReportName(String ReportName) 
	{
	 this.ReportName = ReportName;	
	}
	public void settypeofreport(String typeofreport) 
	{
	 this.typeofreport = typeofreport;
	}
	public void setKey(String key) 
	{
	 this.Key = key;	
	}
	public void setValue(String Value) 
	{
	 this.Value = Value;	
	}

	public void setreporton(String reporton) 
	{
		
	}
	
	
	public static void main(String[] args) 
	{
		Expression exp = new Expression();
		exp.initPool();
		String buffer[] ={"tid	item	qty	rate	",
					"10	aaa	10	12	"};		
		exp.setBuffer(buffer);
		exp.table.put("column", "amount");
		exp.table.put("expression","rate*qty");
		exp.getResult();
		buffer = exp.getBuffer();
		for(int i=0;i<buffer.length;i++)
		{
			System.out.println(buffer[i]);
		}
	}
}
